﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.IO;
using System.Collections.Generic;
using System.Runtime.Serialization.Json;
//using SFWS.XJSON.Core;


namespace JKF.Substructure
{
    public static class Utils
    {
        /// <summary>
        /// 得到ActionName
        /// </summary>
        public static string GetActionName
        {
            get
            {
                return string.IsNullOrEmpty(HttpContext.Current.Request["ActionName"]) ? "" : HttpContext.Current.Request["ActionName"];
            }

        }

        /// <summary>
        /// 取得时间格式
        /// </summary>
        public static string GetDateFormat
        {
            get { return "yyyy-MM-dd"; }
        }

        /// <summary>
        /// 取得时间格式
        /// </summary>
        public static string GetDateTimeFormat
        {
            get { return "yyyy-MM-dd HH:mm:ss"; }
        }

        /// <summary>
        /// 每页显示条数
        /// </summary>
        public static int GetPageSize
        {
            get { return 8; }
        }

        /// <summary>
        /// 将DataTable转换为select选项
        /// </summary>
        /// <param name="dt">dt</param>
        /// <param name="ValueRow"></param>
        /// <param name="TextRow"></param>
        /// <returns></returns>
        public static string DropDownList(DataTable dt, string ValueRow, string TextRow)
        {
            StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in dt.Rows)
            {
                sb.AppendFormat("<option value=\"{0}\">{1}</option>", dr[ValueRow], dr[TextRow]);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 将DataRows转换为select选项
        /// </summary>
        /// <param name="drs"></param>
        /// <param name="ValueRow"></param>
        /// <param name="TextRow"></param>
        /// <returns></returns>
        public static string DropDownList(DataRow[] drs, string ValueRow, string TextRow)
        {
            StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in drs)
            {
                sb.AppendFormat("<option value=\"{0}\">{1}</option>", dr[ValueRow], dr[TextRow]);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 到客户端消息转义
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static string Append(string val)
        {
            if (string.IsNullOrEmpty(val))
                return "";
            StringBuilder sb = new StringBuilder();
            foreach (char c in val)
            {
                switch (c)
                {
                    case '\"':
                        sb.Append(@"\""");
                        break;
                    case '\\':
                        sb.Append(@"\\");
                        break;
                    case '\b':
                        sb.Append(@"\b");
                        break;
                    case '\f':
                        sb.Append(@"\f");
                        break;
                    case '\n':
                        sb.Append(@"\n");
                        break;
                    case '\r':
                        sb.Append(@"\r");
                        break;
                    case '\t':
                        sb.Append(@"\t");
                        break;
                    default:
                        int i = c;
                        if (i < 32 || i >= 127)
                        {
                            sb
                                .Append(@"\u")
                                .Append(i.ToString("X").PadLeft(4, '0'));
                        }
                        else
                        {
                            sb.Append(c);
                        }
                        break;
                }
            }

            return sb.ToString();
        }

        /// <summary>
        /// 客户端的消息
        /// </summary>
        /// <param name="error">是否错误</param>
        /// <param name="message">需要显示的消息</param>
        /// <returns></returns>
        public static string ToClientMessage(bool error, string message)
        {
            return "{error:" + error.ToString().ToLower() + ",message:\"" + Append(message) + "\"}";
        }

        /// <summary>
        /// 显示到客户端的消息
        /// </summary>
        /// <param name="error">是否错误</param>
        /// <param name="message">需要显示的消息</param>
        public static void ToClient(bool error, string message)
        {

            HttpContext.Current.Response.Write(ToClientMessage(error, message));

        }

        /// <summary>
        /// 根据生日取得年龄
        /// </summary>
        /// <param name="birthday">出生日期</param>
        /// <returns></returns>
        public static decimal GetAge(DateTime birthday)
        {
            decimal age = DateTime.Now.Year - birthday.Year;

            if (DateTime.Now.Month > birthday.Month)
            {
                age -= 1;
            }
            else if (DateTime.Now.Month == birthday.Month)
            {
                if (DateTime.Now.Day > birthday.Day)
                {
                    age -= 1;
                }
            }


            return age;
        }

        /// <summary>
        /// 取得对象参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="paramName">参数名</param>
        /// <returns></returns>
        static public T GetParamObj<T>(string paramName)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(System.Web.HttpContext.Current.Request.Params[paramName].ToString())))
            {
                return serializer.ConvertToType<T>(ds.ReadObject(ms));
            }
        }

        /// <summary>
        /// 取得对象参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="paramName">参数名</param>
        /// <returns></returns>
        static public List<T> GetParamObjList<T>(string paramName)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(List<T>));
            using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(System.Web.HttpContext.Current.Request.Params[paramName].ToString())))
            {
                return serializer.ConvertToType<List<T>>(ds.ReadObject(ms));
            }
        }

    }


}

